package com.sc.nft.dao;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.SandTransferRecord;
import com.sc.nft.enums.WalletChannelEnum;
import com.sc.nft.mapper.SandTransferRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 衫德转帐记录表(sc_sand_transfer_record)数据DAO
 *
 * @author zxy
 * @since 2023-01-01 18:54:35
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class SandTransferRecordDao extends ServiceImpl<SandTransferRecordMapper, SandTransferRecord> {



    public Page<SandTransferRecord> getPage(Long userId, Date queryTime, int pageNo, int pageSize) {
        LambdaQueryWrapper<SandTransferRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SandTransferRecord::getUserId,userId);
        if(queryTime!=null){
            wrapper.between(SandTransferRecord::getApplyTime, DateUtil.beginOfMonth(queryTime), DateUtil.endOfMonth(queryTime));
        }
        wrapper.orderByDesc(SandTransferRecord::getId);
        return page(new Page<>(pageNo,pageSize),wrapper);
    }


    public List<SandTransferRecord> transferList(){
        LambdaQueryWrapper<SandTransferRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SandTransferRecord::getApprovalStatus,1);
        wrapper.eq(SandTransferRecord::getWithdrawStatus,0);
        wrapper.last(" limit 200");
        return list(wrapper);
    }

    public Page<SandTransferRecord> adminPage(WalletChannelEnum channel,Long userId,String customerOrderNo,Date startTime,Date endTime,Integer approvalStatus,Integer withdrawStatus,int pageNo,int pageSize){
        LambdaQueryWrapper<SandTransferRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId!=null,SandTransferRecord::getUserId,userId);
        wrapper.eq(StrUtil.isNotBlank(customerOrderNo),SandTransferRecord::getCustomerOrderNo,customerOrderNo);
        if(channel!=null){
            wrapper.eq(SandTransferRecord::getChannel,channel);
        }
        wrapper.eq(approvalStatus!=null,SandTransferRecord::getApprovalStatus,approvalStatus);
        wrapper.eq(withdrawStatus!=null,SandTransferRecord::getWithdrawStatus,withdrawStatus);
        wrapper.le(endTime!=null,SandTransferRecord::getApplyTime,endTime);
        wrapper.ge(startTime!=null,SandTransferRecord::getApplyTime,startTime);
        wrapper.orderByDesc(SandTransferRecord::getId);
        return page(new Page<>(pageNo,pageSize),wrapper);
    }

    public BigDecimal getSumTransfer() {
        return baseMapper.getSumTransfer();
    }

    public BigDecimal getSumCharge() {
        return baseMapper.getSumCharge();
    }

    public BigDecimal getToDayTransferAmount() {
        return baseMapper.getToDayTransferAmount();
    }

    public BigDecimal getToDayTransferCharge() {
        return baseMapper.getToDayTransferCharge();
    }

    public BigDecimal getToDayTransferChargeByDay(String day) {
        return baseMapper.getToDayTransferChargeByDay(day);
    }

    public BigDecimal getToDayTransferAmountByDay(String day) {
        return baseMapper.getToDayTransferAmountByDay(day);
    }

    public String getSumAmount(Date startTime,Date endTime){
        QueryWrapper<SandTransferRecord> wrapper = new QueryWrapper<>();
        wrapper.select("sum(amount) as amount");
        wrapper.eq("approval_status",1);
        wrapper.eq("withdraw_status",1);
        wrapper.between("deal_time",startTime,endTime);
        return getOne(wrapper).getAmount().setScale(2, RoundingMode.HALF_UP).toPlainString();

    }

    /**
     * 获取国库出金
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    public BigDecimal getNationalTreasuryOutMoney(Long userId, Date startTime, Date endTime) {
        return baseMapper.getNationalTreasuryOutMoney(userId,startTime,endTime);
    }
}